package com.unisedu.pay.controller;

import com.unisedu.pay.entity.Global;
import com.unisedu.pay.entity.Order;
import com.unisedu.pay.service.OrderService;
import com.unisedu.pay.utils.IpUtil;
import com.unisedu.pay.utils.MD5Utils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping(path = "code",method = RequestMethod.POST)
    public Map<String, Object> getPayCode(Order order,HttpServletRequest request) throws Exception {
        order.setIp(IpUtil.getIpAddr(request));
        return orderService.getPayCode(order);
    }

    @RequestMapping(path = "wxNotify")
    public void wxNotify(HttpServletRequest request, HttpServletResponse response){
        orderService.parseWXNofity(request,response);
    }

    @RequestMapping(path = "alipayNotify")
    public void alipayNotify(HttpServletRequest request, HttpServletResponse response){
        orderService.parseAlipayNotify(request,response);

    }

    @RequestMapping(path = "wxWebkit")
    public JSONObject wxWebkitPay(HttpServletRequest request){
        JSONObject json = null;
        String orderNo = request.getParameter("orderNo");
        String ip = request.getRemoteHost();
        Order order = orderService.findByOrderNo(orderNo);
        String prepayId = orderService.wxWebkitPay(order,ip);

        json = new JSONObject();
        json.put("timestamp",new Date().getTime()+"");
        json.put("nonceStr", Global.getNonceStr());
        json.put("prepayId",prepayId);
        String str = "appId=wx35ea369e7e926188&nonceStr="+json.getString("nonceStr")+"&package=prepay_id="+json.getString("prepayId")+"&signType=MD5&timeStamp="+json.getString("timestamp")+"&key=eddf1eab810d21fa83ccd4766d8fa1e0";
        String paySign = MD5Utils.MD5Encode(str,"UTF-8");
        json.put("paySign",paySign);
        return json;
    }

    @RequestMapping(path = "wxOrderQuery")
    public Map<String, Object> wxOrderQuery(HttpServletRequest request){
        String orderNo = request.getParameter("orderNo");
        Order order = orderService.findByOrderNo(orderNo);
        Map<String, Object> dm=orderService.wxOrderQuery(orderNo);
        return dm;
    }

    @RequestMapping(path = "alipayOrderQuery")
    public Map<String, Object> alipayOrderQuery(HttpServletRequest request){
        String orderNo = request.getParameter("orderNo");
        Order order = orderService.findByOrderNo(orderNo);
        Map<String, Object> dm=orderService.alipayOrderQuery(order);
        return dm;
    }
    @RequestMapping(path = "hasOrder")
    public ResponseEntity hasOrder(HttpServletRequest request){
        List<Order> order = null;
        order = orderService.findByUserIdAndIdProject(request.getParameter("userId"),request.getParameter("idProject"));
        System.out.println(order);
        if(order != null){
            return new ResponseEntity(HttpStatus.OK);
        }else{
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
    }
    @RequestMapping(path = "hasOrderPay")
    public ResponseEntity hasOrderPay(HttpServletRequest request){
        List<Order> order = null;
        order = orderService.findByUserIdAndIdProject(request.getParameter("userId"),request.getParameter("idProject"));
        System.out.println(order);
        if(order != null){
            return new ResponseEntity(HttpStatus.OK);
        }else{
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }


    }

    @RequestMapping(path = "findByUserId")
    public List<Order> findByUserId(HttpServletRequest request){
        return orderService.findOrdersByOpenId(request.getParameter("userId"));
    }

    @RequestMapping(path = "findByUserIdAndIdProject")
    public List<Order> findByUserIdAndIdProject(HttpServletRequest request){
        return orderService.findByUserIdAndIdProject(request.getParameter("userId"),request.getParameter("idProject"));

    }

    @RequestMapping(path = "findOrdersByOpenId")
    public List<Order> findOrdersByOpenId(HttpServletRequest request){
        return orderService.findOrdersByOpenId(request.getParameter("openid"));
    }
}
